import {
  getCustomerType,
  getPaymentTerm,
  getCustomerOptions,
  loadChannels,
  loadCountrys,
  loadFbaAddress,
  loadSurchargOptions
} from '@/api/base'

export default {
  state: {
    customerType: {},
    paymentTerm: {},
    customerOptions: [],
    channelOptions: [],
    countrys: [],
    fbaAddressOption: [],
    surchargeOptions: []
  },
  mutations: {
    setCustomerType(state, customerType) {
      state.customerType = customerType
    },
    setPaymentTerm(state, paymentTerm) {
      state.paymentTerm = paymentTerm
    },
    setCustomerOptions(state, customerOptions) {
      state.customerOptions = customerOptions
    },
    setChannelOptions(state, channelOptions) {
      state.channelOptions = channelOptions
    },
    setCountrys(state, data) {
      state.countrys = data
    },
    setFbaAddress(state, data) {
      state.fbaAddressOption = data
    },
    setSurchargOptions(state, data) {
      state.surchargeOptions = data
    }
  },
  getters: {
  },
  actions: {
    getCustomerType({ commit }) {
      return new Promise((resolve, reject) => {
        getCustomerType().then(res => {
          if (res.success) {
            const customerType = res.data
            commit('setCustomerType', customerType)
            resolve()
          }
        })
      })
    },
    getPaymentTerm({ commit }) {
      return new Promise((resolve, reject) => {
        getPaymentTerm().then(res => {
          if (res.success) {
            const paymentTerm = res.data
            commit('setPaymentTerm', paymentTerm)
            resolve()
          }
        })
      })
    },
    getCustomerOptions({ commit }) {
      return new Promise((resolve, reject) => {
        getCustomerOptions().then(res => {
          if (res.success) {
            const customerOptions = res.data
            commit('setCustomerOptions', customerOptions)
            resolve()
          }
        })
      })
    },
    getChannelOptions({ commit }) {
      return new Promise((resolve, reject) => {
        loadChannels().then(res => {
          if (res.success) {
            const channelOptions = res.data
            commit('setChannelOptions', channelOptions)
            resolve()
          }
        })
      })
    },
    getCountrys({ commit }) {
      return new Promise((resolve, reject) => {
        loadCountrys().then(res => {
          if (res.success) {
            const data = res.data
            commit('setCountrys', data)
            resolve()
          }
        })
      })
    },
    getFbaAddress({ commit }) {
      return new Promise((resolve, reject) => {
        loadFbaAddress().then(res => {
          if (res.success) {
            const data = res.data
            commit('setFbaAddress', data)
            resolve()
          }
        })
      })
    },
    getSurchargOptions({ commit }) {
      return new Promise((resolve, reject) => {
        loadSurchargOptions().then(res => {
          if (res.success) {
            const data = res.data
            commit('setSurchargOptions', data)
            resolve()
          }
        })
      })
    }
  }
}
